/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.sensor.area;

import java.util.concurrent.FutureTask;
import java.util.logging.Level;
import nl.cloudfarming.client.logging.AppLogFactory;
import nl.cloudfarming.client.logging.AppLogger;
import org.openide.util.Cancellable;

/**
 * Future wrapping the LinkSensorField task and perform a callback when
 * then task has finished
 * 
 * @author Timon Veenstra
 */
public class LinkSensorFieldFuture extends FutureTask<Object> implements Cancellable {

    private static final AppLogger LOG = AppLogFactory.getLogger(LinkSensorFieldFuture.class);
    private final LinkSensorFieldTask task;
    private final LinkSensorTask.Callback callback;

    LinkSensorFieldFuture(LinkSensorFieldTask task, LinkSensorTask.Callback callback) {
        super(task);
        this.task = task;
        this.callback = callback;
    }

    @Override
    public void run() {
        LOG.log(Level.FINE, "run invoked on future for task {0}", task.toString());
        task.setCancellable(this);
        super.run();
    }

    @Override
    public boolean cancel() {
        LOG.log(Level.FINE, "cancel invoked on future for task {0}", task.toString());
        return cancel(true);
    }

    @Override
    protected void done() {
        LOG.log(Level.FINE, "task {0} is done", task.toString());
        callback.done();
    }
}
